package com.fagejiang.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * TODO
 *
 * @author 发哥讲Java
 * @version 1.0
 * @date 2021-05-17 13:37
 */
@Component
@Slf4j
public class CustomGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入到 global filter 1111111" + exchange);
        System.out.println("进入到 global filter 1111111" + chain);

        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String url = request.getPath().value();

        System.out.println("url = " + url);

        // 调用 chain.filter(exchange) 通过此 过滤器,
        // return java.lang.NullPointerException: The Mono returned by the supplier is null ,异常返回
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
